<?php

$dbhost = "mysql.groovemechanic.net";
$dbuser = "oproto_user";
$dbpass = "oproto12345";
$dbname = "groovemechanic_oproto";

$con = mysql_connect ($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db ($dbname) or die (mysql_error());

$docname = $_POST['name'];
$username = trim($_POST['username']);
$jsonstr = mysql_real_escape_string($_POST['json']);
$author = mysql_real_escape_string($_POST['author']);
$title = mysql_real_escape_string($_POST['title']);
$description = mysql_real_escape_string($_POST['description']);

$js = mysql_real_escape_string($_POST['js']);
$cssClasses = mysql_real_escape_string($_POST['cssClasses']);

if ($username == "") {
	$username = "NoName";
}
$query = "SELECT * FROM documents WHERE name = '$docname' AND username='$username'";


$result = mysql_query($query) or die(mysql_error() . "\n" . $jsonstr);

if (mysql_num_rows($result) > 0) {
	$row = mysql_fetch_array($result);
	$docid = $row[id];
	
	//$query = "UPDATE documents SET json='$jsonstr' WHERE id='$docid'";
	$query = "UPDATE documents SET json='$jsonstr',author='$author',title='$title',description='$description',js='$js',cssclasses='$cssClasses' WHERE id=$docid";
	
	$result = mysql_query($query) or die(mysql_error() . "\n" . $query);
	
} else {
	
	//$query = "INSERT INTO documents (name, json, username) VALUES('$docname', '$jsonstr', '$username')";
	$query = "INSERT INTO documents (name, json, username, author, title, description, js, cssClasses) VALUES('$docname', '$jsonstr', '$username', '$author', '$title', '$description', '$js', '$cssClasses')";
	$result = mysql_query($query) or die(mysql_error() . "\n" . $query);
	
}

mysql_close($con);

?>
